<%@page contentType="application/json;charset=UTF-8" %><%@page import="be.destin.skos.core.SkosManager" %><%@page import="be.destin.skos.html.SkosHtmlManager" %><%@page import="be.destin.skos.core.SchemeInterface" %><%@page import="be.destin.skos.core.Concept" %><%@page import="be.destin.skos.core.Application" %><%@page import="be.destin.skos.view.SkosView" %><%@page import="be.destin.skos.view.SkosViewType" %><%@page import="be.destin.skos.view.SkosViewManager" %><%
	SkosHtmlManager skosHtmlManager = SkosHtmlManager.getAskosiSessionInstance(request);
	SkosManager skosManager = skosHtmlManager.getSkosManager();
	String language = skosHtmlManager.getLanguage();
	SchemeInterface aScheme = skosManager.getApplications();
/* DO NOT WORK BECAUSE REVERSE PROXY!
	String reqURL = request.getRequestURL().toString();
	int pURI =  reqURL.indexOf(request.getRequestURI());
	if (pURI >= 0) 	reqURL = reqURL.substring(0,pURI); // Keep only host of the request
*/
	if (aScheme != null) {
		SkosViewType viewType = SkosViewType.ALFA;
		SkosView aView = SkosViewManager.openView(skosHtmlManager,aScheme.getAbout(), viewType, language,false, null);
		%>var appNames = <%SkosViewManager.renderVocabularyAsJSON(out, aView);%>;
var appSearch = { <%
		boolean first = true;
		for (Concept aConcept : aScheme.getAllConcepts()) {
			if (aConcept instanceof Application) {
				Application anApp = (Application)aConcept;
				String atomURL = anApp.getAtomURL();
				if (atomURL != null && !atomURL.isEmpty()) {
					if (!first) { %>, <% }
					else first = false;
					%>"<%=anApp.getAbout()%>":"<%=atomURL%>"<%
				}
			}
		}
		%>};
var appKeys = { <%
		          		first = true;
		          		for (Concept aConcept : aScheme.getAllConcepts()) {
		          			if (aConcept instanceof Application) {
		          				Application anApp = (Application)aConcept;
		          				String appKey = anApp.getMethodToken();
		          				if (appKey != null && !appKey.isEmpty()) {
			          				if (!first) { %>, <% }
			          				else first = false;
		          					%>"<%=anApp.getAbout()%>":"<%=appKey%>"<%
		          				}
		          			}
		          		}
		          		%>};

<%	}
	aScheme = skosManager.getRoles();
	if (aScheme != null) {
		SkosViewType viewType = SkosViewType.ALFA;
		SkosView aView = SkosViewManager.openView(skosHtmlManager,aScheme.getAbout(), viewType, language,false, null);
		%>var roleNames = <%SkosViewManager.renderVocabularyAsJSON(out, aView);%>;
<%
	}
	aScheme = skosManager.getMetaScheme();
	if (aScheme != null) {
		SkosViewType viewType = SkosViewType.ALFA;
		SkosView aView = SkosViewManager.openView(skosHtmlManager,aScheme.getAbout(), viewType, language,false, null);
		%>var schemeNames = <%SkosViewManager.renderVocabularyAsJSON(out, aView);%>;<%
	}
	aScheme = skosManager.getScheme("mark");
	if (aScheme != null) {
		SkosViewType viewType = SkosViewType.ALFA;
		SkosView aView = SkosViewManager.openView(skosHtmlManager,aScheme.getAbout(), viewType, language,false, null);
		%>var markNames = <%SkosViewManager.renderVocabularyAsJSON(out, aView);%>;<%
	}
%>

// Browse an array until the function returns true 
function ArrayIndexOf(a, fnc) {
	if (!fnc || typeof (fnc) != 'function') {
		return -1;
	}
	if (!a || !a.length || 1 >= a.length) return -1;
	for (var i = 0; i < a.length; i++) {
		if (fnc(a[i])) return i;
	}
	return -1;
}

// Concept (got in JSON) to describe an Indexation Role 
function findRole(name) {
	var i = ArrayIndexOf(roleNames.skosView.narrower, function(obj) {
			return obj.about == name;
		});
	if (-1 != i) {
		return roleNames.skosView.narrower[i]
	}
	return {"prefLabel":name};
}

// Concept (got in JSON) to describe a linked Application 
function findApp(name) {
	var i = ArrayIndexOf(appNames.skosView.narrower, function(obj) {
			return obj.about == name;
		});
	if (-1 != i) {
		return appNames.skosView.narrower[i]
	}
	return {"prefLabel":name};
}

var uriHere = new Uri(location.href);

// provide an URL without parameters for comparisons
function cleanUrl(url) {
	if("string" == typeof url) {
		var pDot = url.indexOf('{');
		if (pDot >= 0) url = url.substr(0,pDot);
		pDot = url.indexOf('?');
		if (pDot >= 0) url = url.substr(0,pDot);
		var uri = new Uri(url);
		uri.setProtocol("");
		if (uri.host()==uriHere.host()
			&& uri.port()==uriHere.port()) {
			uri.setHost("");
			uri.setPort("");
		}
		url = uri.toString();
		pDot = url.indexOf('//'); // uri.toString may leave a double slash
		if (pDot >= 0) {
			url = url.substr(pDot+2);
		}
		pDot = url.indexOf('/proxy/');
		if (pDot >= 0) url = url.substr(pDot+7);
		if(url.length > 0) {
			if(url.charAt( url.length-1 ) == "/") {
				url = url.slice(0, -1)
			}
		}
	}
	return url;
}

var appSearchClean = new Array();
for (var x in appSearch) {
	appSearchClean[x] = cleanUrl(appSearch[x]);
}

//Concept (got in JSON) to describe a linked Application 
function findApplication(url) {
	var cleanU = cleanUrl(url);
	for (var key in appSearch) {
		if (typeof key != "string") continue;
		if (cleanU === appSearchClean[key]) {
			return key;
		}
	}
	return "";
}

// MetaData (got in JSON) to describe a ConceptScheme found by its code 
function findScheme(name) {
	var i = ArrayIndexOf(schemeNames.skosView.narrower, function(obj) {
			return obj.about == name;
		});
	if (-1 != i) {
		return schemeNames.skosView.narrower[i]
	}
	return {"prefLabel":name};
}

//MetaData (got in JSON) to describe a ConceptScheme found by its URI. result.about gives the code 
function findSchemeUri(url) {
	var i = ArrayIndexOf(schemeNames.skosView.narrower, function(obj) {
			return obj.uri == url;
		});
	if (-1 != i) {
		return schemeNames.skosView.narrower[i]
	}
	return {"uri":url};
}

//MetaData (got in JSON) to describe a Concept found by its URI. result.about gives the code 
function findSchemeNamespace(url) {
	var i = ArrayIndexOf(schemeNames.skosView.narrower, function(obj) {
		    if (obj.namespace == null || obj.namespace.length == 0) return false;
			return url.indexOf(obj.namespace) == 0;
		});
	if (-1 != i) {
		return schemeNames.skosView.narrower[i]
	}
	return {"uri":url};
}

// Concept (got in JSON) for an action icon in ASKOSI/Glips UI 
function findMark(name) {
	var i = ArrayIndexOf(schemeNames.skosView.narrower, function(obj) {
			return obj.about == name;
		});
	if (-1 != i) {
		return markNames.skosView.narrower[i]
	}
	return {"prefLabel":name};
}

var prevApp = '';

// Small visual ID for an application:indexationrole: its HTML icon if it exists and its name: (in the user language) if not.
function decoAppRole(appRoleName) {
	var i = appRoleName.indexOf(':',0);
	if (i < 0) return appRoleName;
	var app = appRoleName.substr(0,i);
	if (app != prevApp) {
		prevApp = app;
		var a = findApp(app);
		app = (a.icon ? ('\<img src=\"'+a.icon+'\" title=\"'+a.prefLabel+'\" border=0\/\>') : (a.prefLabel+':'));
	} else {
		app = '';
	}
	var r = findRole(appRoleName.substr(i+1,999));
	return app + ( r.icon ?  ('\<img src=\"'+r.icon+'\" title=\"'+r.prefLabel+'\" border=0\/\>') : r.prefLabel );
}

// Small visual ID for a scheme: its HTML icon if it exists and its name: (in the user language) if not.
function decoScheme(schemeCode) {
	var a = findScheme(schemeCode);
	return (a.icon ? ('\<img src=\"'+a.icon+'\" title=\"'+a.prefLabel+'\" border=0\/\>') : (a.prefLabel+':'));
}

// From a Scheme and a Concept Code, build their URL id
function SchemeConcept2Url(schemeCode, conceptCode) {
	var a = findScheme(schemeCode);
	return a.namespace+conceptCode;
}

//From a Scheme, build its URL id
function Scheme2Url(schemeCode) {
	var a = findScheme(schemeCode);
	return a.uri;
}

//From an URL and a Scheme to one of its Concepts
function UrlScheme2Concept(url, schemeCode) {
	var a = findScheme(schemeCode);
	var nam = a.namespace;
	if (nam == null || nam.length == 0) return null;
	if (url.indexOf(nam) != 0) return null;
	return url.substring(nam.length);
}

//From an URL to a Scheme and its Concept
function Url2Scheme_Concept(url) {
	var a = findSchemeNamespace(url);
	var nam = a.namespace;
	if (nam == null || nam.length == 0) return null;
	return a.about+'_'+url.substring(nam.length);
}
